package com.alibaba.otter.shared.arbitrate.autoconfigure;

import com.alibaba.otter.shared.arbitrate.impl.setl.ArbitrateFactory;
import com.alibaba.otter.shared.arbitrate.impl.setl.monitor.NodeMonitor;
import com.alibaba.otter.shared.arbitrate.impl.zookeeper.ZooKeeperClient;
import com.alibaba.otter.shared.common.utils.thread.ExecutorServiceFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.concurrent.ExecutorService;

@Configuration(proxyBeanMethods = false)
public class ArbitrateCommonConfiguration {



    @Bean
    public ArbitrateFactory arbitrateFactory(){
        return new ArbitrateFactory();
    }

    @Bean
    public ExecutorServiceFactoryBean arbitrateExecutor(){
        ExecutorServiceFactoryBean executorServiceFactoryBean = new ExecutorServiceFactoryBean();
        executorServiceFactoryBean.setPoolSize(20);
        executorServiceFactoryBean.setAcceptCount(40);
        executorServiceFactoryBean.setName("Otter-Arbitrate-Executor");
        return executorServiceFactoryBean;
    }

    @Bean
    @DependsOn("arbitrateFactory")
    public ZooKeeperClient zookeeperClient(@Value("${otter.zookeeper.cluster.default:localhost:2181}") String cluster){
        ZooKeeperClient zooKeeperClient = new ZooKeeperClient();
        zooKeeperClient.setCluster(cluster);
        zooKeeperClient.setSessionTimeout(60000);
        return zooKeeperClient;
    }

    @Bean
    public NodeMonitor nodeMonitor(@Qualifier("arbitrateExecutor")ExecutorService executorService){
        NodeMonitor nodeMonitor = new NodeMonitor();
        nodeMonitor.setArbitrateExecutor(executorService);
        return nodeMonitor;
    }


}
